import Koa from 'koa'
import BodyParser from 'koa-bodyparser'
import Static from 'koa-static'
import Path from 'path'
import KoaBody from 'koa-body'
import AddressIP from 'ip'
import Routes from './routes/index'
import Logger from './middlewares/logger/logger'
import Permission from './middlewares/permission/permission'
import { warn } from './utils/log'

const PORT = 5000
const app = new Koa()

app.use(
  KoaBody({
    multipart: true,
    formidable: {
      uploadDir: Path.join(__dirname, '../public/uploads'),
      keepExtensions: true,
    },
  }),
)

app.use(BodyParser())

// 静态资源
app.use(Static(`${Path.join(__dirname)}/public`))

// middleware registion
app.use(Logger)
app.use(Permission)

// 路由
app.use(Routes.routes())

app.listen(PORT, () => {
  warn(`[start at] http://${AddressIP.address()}:${PORT}`)
  warn(`[start at] http://localhost:${PORT}`)
})

module.exports = app
